package com.example.servlet;

import com.example.dao.StudentDao;
import com.example.entity.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    private StudentDao studentDao = new StudentDao();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // 设置请求和响应的字符编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        // 获取表单数据
        String studentId = request.getParameter("studentId");
        String name = request.getParameter("username");
        String grade = request.getParameter("grade");
        String classes = request.getParameter("class");
        String email = request.getParameter("email");
        System.out.println("==============");
        System.out.println("注册信息：");
        System.out.println(studentId);
        System.out.println(name);
        System.out.println(grade);
        System.out.println(classes);
        System.out.println(email);


        // 验证数据
        if (studentId == null || name == null || grade == null ||
                classes == null || email == null ||
                studentId.trim().isEmpty() || name.trim().isEmpty() ||
                grade.trim().isEmpty() || classes.trim().isEmpty() ||
                email.trim().isEmpty()) {

            response.getWriter().write("所有字段都必须填写！");
            return;
        }

        // 检查学号是否已存在
        if (studentDao.isStudentIdExists(studentId)) {
            response.getWriter().write("该学号已被注册！");
            return;
        }
        // 验证输入数据
        if (name.trim().isEmpty()) {
            request.setAttribute("error", "用户名不能为空！");
            request.getRequestDispatcher("login.jsp").forward(request, response);
            return;
        }

        if (email.trim().isEmpty()) {
            request.setAttribute("error", "邮箱不能为空！");
            request.getRequestDispatcher("login.jsp").forward(request, response);
            return;
        }

        // 验证用户名长度
        if (name.length() < 2 || name.length() > 20) {
            request.setAttribute("error", "用户名长度必须在2-20个字符之间！");
            request.getRequestDispatcher("login.jsp").forward(request, response);
            return;
        }

        // 验证邮箱格式
        if (!email.matches("^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$")) {
            request.setAttribute("error", "邮箱格式不正确！");
            request.getRequestDispatcher("login.jsp").forward(request, response);
            return;
        }

        // 创建学生对象
        Student student = new Student(studentId, name, grade, classes, email);

        // 保存到数据库
        if (studentDao.addStudent(student)) {
            response.getWriter().write("注册成功！");
            //请求转发
            request.getRequestDispatcher("login.jsp").forward(request, response);
            //response.sendRedirect("login.jsp");
        } else {
            response.getWriter().write("注册失败，请稍后重试！");
             response.sendRedirect("register.jsp");
        }
    }
} 